package com.zq.behavior.iterator;

import java.util.stream.IntStream;

class IntArray implements Aggregate<Integer> {
    private final int[] array;

    public IntArray(int[] array) {
        this.array = array;
    }


    @Override
    public Iterator<Integer> createIterator() {
        return new AggregateIterator(array);
    }

    static class AggregateIterator implements Iterator<Integer> {
    private final int[] array;
    private int index;

        AggregateIterator(int[] array) {
            this.array = array;
            index = 0;
        }

        @Override
        public Integer first() {
            return array[0];
        }

        @Override
        public Integer next() {
            return array[++index];
        }

        @Override
        public boolean hasNext() {
            return index + 1 < array.length;
        }

        @Override
        public Integer currentItem() {
            return array[index];
        }
    }
}

public class Client {
    public static void main(String[] args) {
        int[] array = IntStream.rangeClosed(0, 10).toArray();
        IntArray intArray = new IntArray(array);

        Iterator<Integer> iterator = intArray.createIterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
    }
}
